package com.fin.zw.aiqas.controller;


import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.Resource;
import com.fin.zw.aiqas.model.data.ResourceModel;
import com.fin.zw.aiqas.model.request.ResourceRequest;
import com.fin.zw.aiqas.model.response.ResourceResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(value = "菜单管理",description = "菜单管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的菜单信息",notes = "查询所有的菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<Resource>> queryResourceAll(Pageable pageable,
                                                           Resource request){
        Page<Resource> page = resourceService.queryResourceAll(pageable,request);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的菜单信息",notes = "查询所有的菜单信息")
    public ResponseEntity<List<Resource>> getResourceList(@ApiIgnore @CurrentUser SessionModel sessionModel){
        List<Resource> list = resourceService.getResourceList(sessionModel);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/fetchList")
    @ApiOperation(value = "查询所有的菜单信息(分类型)", notes = "查询所有的菜单信息(分类型)")
    public ResponseEntity<ResourceResponse> getResourceResponseList(@ApiIgnore @CurrentUser SessionModel sessionModel) {
        ResourceResponse result = resourceService.getResourceResponses(sessionModel);
        return ResponseEntity.ok().body(result);
    }

    @PostMapping("/add")
    @ApiOperation(value = "菜单信息的添加",notes = "菜单信息的添加")
    public ResponseEntity<String> createResource(@RequestBody Resource resource){
        resourceService.createResource(resource);
        return ResponseEntity.ok().body("添加成功");
    }
    @RequestMapping(value = "/update",method = {RequestMethod.PUT,RequestMethod.PATCH})
    @ApiOperation(value = "菜单信息的修改",notes = "菜单信息的修改")
    public ResponseEntity<String> updateResource(@RequestBody Resource resource,
                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        resourceService.updateResource(resource, sessionModel);
        return ResponseEntity.ok().body("修改成功");
    }

    @PostMapping("/updateName")
    @ApiOperation(value = "菜单信息的修改", notes = "菜单信息的修改")
    public ResponseEntity<String> updateName(@RequestBody ResourceModel request,
                                             @ApiIgnore @CurrentUser SessionModel sessionModel) {
        resourceService.updateResourceName(request, sessionModel);
        return ResponseEntity.ok().body("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "菜单信息删除",notes = "菜单信息删除")
    public ResponseEntity<String> deleteResource(@PathVariable Long id){
        resourceService.deleteResourceById(id);
        return ResponseEntity.ok().body("删除成功");
    }


    @PostMapping("/moveResource")
    @ApiOperation(value = "资源顺序的调整", notes = "资源顺序的调整")
    public ResponseEntity<String> moveResource(@RequestBody ResourceRequest request,
                                               @ApiIgnore @CurrentUser SessionModel sessionModel) {
        resourceService.moveResource(request, sessionModel);
        return ResponseEntity.ok().body("调整成功");
    }
}
